package com.docMan.utils;



import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.converter.ExcelToHtmlUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.w3c.dom.Document;


/**
 * @author: LOEI
 * @since: May 3, 2013
 * @modified: May 3, 2013
 * @version:
 */
public class Excel2Html {

	public static void main(String argv[]) {
		try {
			convert2Html("D://3.xls","D://3.html");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeFile(String content, String path,String code) {
		FileOutputStream fos = null;
		BufferedWriter bw = null;
		content = content.replaceAll(".b1\\{[\\w*-:]*;}",".b1{text-align:center;white-space-collapsing:preserve;}");//锟斤拷锟斤拷锟斤拷
		content = content.replaceAll("<th>[A-Z]</th>","<th></th>");//去锟斤拷锟酵稟-Z
		content = content.replaceAll("<th class=\"rownumber\">\\d*</th>","<th class=\"rownumber\"></th>");//去锟斤拷锟斤拷斜锟斤拷 1-n
		try {
			File file = new File(path);
			fos = new FileOutputStream(file);
			bw = new BufferedWriter(new OutputStreamWriter(fos,code));
			bw.write(content);
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			try {
				if (bw != null)
					bw.close();
				if (fos != null)
					fos.close();
			} catch (IOException ie) {
			}
		}
	}
	/**
	 *  excel 转换成html 文件 预览
	 * @param fileName
	 * @param outPutFile
	 * @throws Exception
	 */
	public static void convert2Html(String fileName, String outPutFile)
			throws Exception {
		String code=ProjectUtils.codeString(fileName);
		HSSFWorkbook workbook = ExcelToHtmlUtils.loadXls(new File(fileName));
		ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
		excelToHtmlConverter.processWorkbook(workbook);
		/*System.out.println(excelToHtmlConverter.getCssClassPrefixDiv());
		System.out.println(excelToHtmlConverter.getCssClassPrefixRow());
		System.out.println(excelToHtmlConverter.getCssClassPrefixTable());
		System.out.println(excelToHtmlConverter.getCssClassPrefixCell());
		System.out.println(excelToHtmlConverter.getDocument());*/
		Document htmlDocument = excelToHtmlConverter.getDocument();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		DOMSource domSource = new DOMSource(htmlDocument);
		StreamResult streamResult = new StreamResult(out);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer serializer = tf.newTransformer();
//		String codeString=codeString("D://3.xls");
		serializer.setOutputProperty(OutputKeys.ENCODING, code);
		serializer.setOutputProperty(OutputKeys.INDENT, "yes");
		serializer.setOutputProperty(OutputKeys.METHOD, "html");
		serializer.transform(domSource, streamResult);
		out.close();
		writeFile(new String(out.toByteArray()), outPutFile,code);
	}
	
	/** 
	 * 鍒ゆ柇鏂囦欢鐨勭紪鐮佹牸寮�
	 * @param fileName :file 
	 * @return 鏂囦欢缂栫爜鏍煎紡 
	 * @throws Exception 
	 */  
	public static String codeString(String fileName) throws Exception{  
	    BufferedInputStream bin = new BufferedInputStream(  
	    new FileInputStream(fileName));  
	    int p = (bin.read() << 8) + bin.read();  
	    String code = null;  
	      
	    switch (p) {  
	        case 0xefbb:  
	            code = "UTF-8";  
	            break;  
	        case 0xfffe:  
	            code = "Unicode";  
	            break;  
	        case 0xfeff:  
	            code = "UTF-16BE";  
	            break;  
	        default:  
	            code = "GBK";  
	    }  
	      
	    return code;  
	}  
}